
<%@ page import="mybatis.pojo.Student" %>
<%@ page import="mybatis.services.StudentService" %>
<%@ page import="mybatis.util.StrUtil" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.UUID" %>
<%--
  Created by IntelliJ IDEA.
  User: Dell
  Date: 2022/7/4
  Time: 17:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    DiskFileItemFactory d = new DiskFileItemFactory();
    ServletFileUpload sp = new ServletFileUpload(d);
    sp.setHeaderEncoding("utf-8");
    List<FileItem> fileItems = sp.parseRequest(request);

    Student student = new Student();

    for (FileItem f : fileItems) {
        out.print("<p>");
        if (f.isFormField()){
            out.print("表单：名字是" + f.getFieldName() + "值是" + f.getString("utf-8"));
            switch (f.getFieldName()){
                case "stuno":
                    student.setStuno(Long.parseLong(f.getString("utf-8")));

                    break;
                case "sname":
                    student.setSname(f.getString("utf-8"));
                    break;
                case "gender":
                    student.setSex(f.getString("utf-8"));
                    break;
                case "tel":
                    student.setTelephone(f.getString("utf-8"));
                    break;
                case "qq":
                    student.setQq(f.getString("utf-8"));
                    break;
                case "wechat":
                    student.setWechat(f.getString("utf-8"));
                    break;
                case "gradeid":
                    student.setGradeid(StrUtil.toInt(f.getString("utf-8")));
                    break;
                case "enterDate":
                    student.setEnterdate(StrUtil.toDate(f.getString("utf-8")));
                    break;
                case "city":
                    student.setFromcity(f.getString("utf-8"));
                    break;
                case "birth":
                    student.setBirthday(StrUtil.toDate(f.getString("utf-8")));
                    break;
                case "address":
                    student.setAddress(f.getString("utf-8"));
                    break;
                case "psd":
                    student.setPass(f.getString("utf-8"));
                    break;
            }
        }else {
            out.print("这是一个文件域，名字是" + f.getFieldName());
            if (f.getSize() < 1)
                continue;
            out.print("文件名是：" + f.getName());
            String fileName = f.getName();
            String houzhui = fileName.substring(fileName.lastIndexOf('.'));
            String newName = UUID.randomUUID() + houzhui;
            File newPic = new File(application.getRealPath("images"));
            if (newPic.exists() == false)
                newPic.mkdirs();

            f.write(new File(newPic, newName));
            student.setIdcardpic("images/" + newName);///!!!!!!!
        }
    }
    out.print("</p>");

    int update = StudentService.ME.update(student);
    System.out.println(student.toString());
    if (update > 0) {
        out.print("修改成功");
    } else {
        out.print("修改失败");

    }

//    request.setCharacterEncoding("utf-8");
//    Long id =Long.parseLong( request.getParameter("id"));
//    out.print(id);
//    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//    request.setCharacterEncoding("utf-8");
//    String sname = request.getParameter("sname");
//    String gender = request.getParameter("gender");
//    String tel = request.getParameter("tel");
//    String qq = request.getParameter("qq");
//    String wechat = request.getParameter("wechat");
//    int gradeId = Integer.parseInt(request.getParameter("gradeId"));
//    Date enterDate = simpleDateFormat.parse(request.getParameter("enterDate"));
//    String city = request.getParameter("city");
//    Date birth = simpleDateFormat.parse(request.getParameter("birth"));
//    String address = request.getParameter("address");
//    String psd = request.getParameter("psd");
//    String idCard = request.getParameter("idCard");
//
//
//    Student student = new Student();
//    student.setStuno(id);
//    student.setSname(sname);
//    student.setSex(gender);
//    student.setTelephone(tel);
//    student.setQq(qq);
//    student.setWechat(wechat);
//    student.setGradeid(gradeId);
//    student.setEnterdate(enterDate);
//    student.setFromcity(city);
//    student.setBirthday(birth);
//    student.setAddress(address);
//    student.setPass(psd);
//    student.setIdcardpic(idCard);
//    int update = StudentService.ME.update(student);
//    if (update > 0) {
//        out.print("修改成功");
//    } else {
//        out.print("修改失败");
//    }
%>

</body>
</html>
